
package com.lzw.queue;

import com.lzw.config.RabbitConfig;
import com.lzw.dto.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Component;

import static com.lzw.config.RabbitConfig.DIRECT11;

/**
 * ***注意已存在的交换机如果修改了绑定关系，需要先删除交换机，否则只会追加绑定关系，废弃的不会自动删除
 * ***修改key是队列名也需要同步修改不然队列名一样key不一样，key作用会失效。
 * ***建议不要修改消费者，直接重新创建。如果必须修改则在管理端把交换机删除
 *
 * @author liuzw
 * @date 2021/7/13
 */
@Description("RabbitMQ消费者-DEMO")
@Component
@Slf4j
public class RabbitConsumer {

    /**
     * 需要提前创建队列或在代码里先声明队列
     *
     * @param message message
     * @see RabbitConfig#publisherSimpleDemo1()
     */
    @RabbitListener(queues = "${publisher.simple.demo1:PUBLISHER.SIMPLE.DEMO1}", concurrency = "${queue.name.demo1.size:10}")
    public void handleMessage1(String message) {
        System.out.println(message);
    }

    /**
     * 无需提前申明
     *
     * @param student Student
     */
    @RabbitListener(queuesToDeclare = @Queue("${publisher.simple.demo2:PUBLISHER.SIMPLE.DEMO2}"), concurrency = "${queue.name.demo1.size:10}")
    public void handleMessage2(Student student) {
        System.out.println(this + "接收到消息：" + student.toString());
    }

    /**
     * fanout交换机把消息路由给交换机下所有队列
     *
     * @param student Student
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${publisher.simple.demo3:PUBLISHER.SIMPLE.DEMO3}", durable = "true"),
            exchange = @Exchange(name = "fanout2", type = ExchangeTypes.FANOUT)
    ), concurrency = "${queue.name.demo1.size:10}")
    public void handleMessage3(Student student) {
        System.out.println(this + "handleMessage3:" + "接收到消息：" + student.toString());
    }

    /**
     * fanout交换机把消息路由给交换机下所有队列
     *
     * @param student Student
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${publisher.simple.demo4:PUBLISHER.SIMPLE.DEMO4}", durable = "true"),
            exchange = @Exchange(name = "fanout2", type = ExchangeTypes.FANOUT)
    ), concurrency = "${queue.name.demo1.size:10}")
    public void handleMessage4(Student student) {
        System.out.println(this + "handleMessage4:" + "接收到消息：" + student.toString());
    }

    /**
     * direct交换机把消息根据KEY路由给交换机下的队列
     * 注意有坑
     *
     * @param student Student
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${publisher.simple.demo5:PUBLISHER.SIMPLE.DEM5}", durable = "true"),
            exchange = @Exchange(name = "direct2", type = ExchangeTypes.DIRECT),
            key = "blue"
    ), concurrency = "${queue.name.demo1.size:10}")
    public void handleMessage5(Student student) {
        System.out.println(this + "handleMessage5:" + "接收到消息：" + student.toString());
    }

    /**
     * direct交换机把消息根据KEY路由给交换机下的队列
     *
     * @param student Student
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${publisher.simple.demo6:PUBLISHER.SIMPLE.DEMO6}", durable = "true"),
            exchange = @Exchange(name = "direct2", type = ExchangeTypes.DIRECT),
            key = "red"
    ), concurrency = "${queue.name.demo1.size:10}")
    public void handleMessage6(Student student) {
        System.out.println(this + "handleMessage6:" + "接收到消息：" + student.toString());
    }

    /**
     * 死信队列
     *
     * @param student Student
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${publisher.simple.demo7:PUBLISHER.SIMPLE.DEMO7}", durable = "true"),
            exchange = @Exchange(name = DIRECT11, type = ExchangeTypes.DIRECT)
    ), concurrency = "${queue.name.demo1.size:10}")
    public void handleMessage7(Student student) {
        log.info("[死信队列消费者],接收到消息：{}", student.toString());
    }

    /**
     * 延迟消息
     *
     * @param student Student
     * @see Exchange#delayed
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "${publisher.simple.demo8:PUBLISHER.SIMPLE.DEMO8}", durable = "true"),
            exchange = @Exchange(name = "direct8", type = ExchangeTypes.DIRECT, delayed = "true")
    ), concurrency = "${queue.name.demo1.size:10}")
    public void handleMessage8(Student student) {
        log.info("[延迟队列消费者],接收到消息：{}", student.toString());
    }
}
